from django.core.paginator import Paginator
from django.shortcuts import render

# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import AllowAny, IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from course.models import Tag, Category, Course
from course.serializers import TagSer, CategorySer, CourseSer, CourseSer1


# 自定义分页器 局部
class PageNum(PageNumberPagination):
    # 查询字符串中代表每页返回数据数量的参数名, 默认值: None
    page_size_query_param = 'page_size'
    # 查询字符串中代表页码的参数名, 有默认值: page
    # page_query_param = 'page'
    # 一页中最多的结果条数
    # max_page_size = 2


# 获取所有标签
class TagView(APIView):
    permission_classes = (AllowAny,)

    # def perform_authentication(self, request):
    #     pass
    def get(self, request):
        queryset = Tag.objects.all()
        ser = TagSer(queryset, many=True)
        return Response({"code": 200, "msg": "数据返回成功", "data": {"taglist": ser.data}})


# 获取所有类别
class CategoryView(APIView):
    permission_classes = (AllowAny,)
    # def perform_authentication(self, request):
    #     pass
    def get(self, request):
        queryset = Category.objects.all()
        ser = CategorySer(queryset, many=True)
        return Response({"code": 200, "msg": "数据返回成功", "data": {"catelist": ser.data}})


class CourseView(APIView):
    permission_classes = (AllowAny,)
    # def perform_authentication(self, request):
    #     pass
    def get(self,request):
        '''
        :param request:# free：类别
# {page:1,size:6,tag_id:parms['tag_id'],free:id,online:""全部/0未上线/1上线,sort:""最新/2最热}
        :return: 返回 data数据，total数据总数
        '''
        # queryset=[]
        page = request.GET.get("page")
        size = request.GET.get("size")
        tag_id = request.GET.get("tag_id")
        cate_id = request.GET.get("free")
        status = request.GET.get("online") #默认都展示
        sort = request.GET.get("sort") #默认最新
        # print(page,size,tag_id,cate_id,status,sort)
        if sort == "":
            sort = "create_time"
        if sort == "2":
            sort = "follow_num"
        if status == "":
            status=1
        if tag_id == "" and cate_id == "":
            queryset = Course.objects.filter(status=status).order_by(sort)
            # count = Course.objects.filter(status=status).count()
        elif tag_id =="" and cate_id !="":
            queryset = Course.objects.filter(status=status,category=cate_id).order_by(sort)
            # count = Course.objects.filter(status=status,category=cate_id).count()
        elif tag_id !="" and cate_id =="":
            queryset = Course.objects.filter(status=status, tag_id=tag_id).order_by(sort)
            # count = Course.objects.filter(status=status, tag_id=tag_id).count()
        elif tag_id != '' and cate_id !='':
            queryset = Course.objects.filter(status=status, tag_id=tag_id ,category=cate_id).order_by(sort)
        paginator = Paginator(queryset,size)
        pagedata=paginator.page(page)
        ser = CourseSer(pagedata,many=True).data
        count = queryset.count()
        print(count)
        return Response({"code":200,"msg":"成功获取数据","data":ser,"total":count})

class courseView(ModelViewSet):
    '''
    产品的增删改查
    '''
    queryset = Course.objects.all()
    serializer_class = CourseSer
    permission_classes = (AllowAny,)
    # 指定过滤方法类, 排序方法类, 一个或多个

    filter_backends = (DjangoFilterBackend, OrderingFilter)  # 同时支持过滤和排序

    # 指定排序字段, 不设置, 排序功能不起效
    ordering_fields = ('follow_num', 'create_time')

    # 指定过滤字段, 不设置, 过滤功能不起效
    filter_fields = ('title', 'category', 'tag', 'status')

    # 自定义分页器 覆盖全局配置
    pagination_class = PageNum

    def get_serializer_class(self):
        if self.action == 'list':
            return CourseSer1
        else:
            return CourseSer
